package logic

import (
	"context"
	"errors"
	"short-vid-joy/models/mysql"

	"short-vid-joy/shortrpc/internal/svc"
	"short-vid-joy/shortrpc/shortrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoShowLogic {
	return &VideoShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoShowLogic) VideoShow(in *shortrpc.VideoShowRequest) (*shortrpc.VideoShowResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var video mysql.Video

	//视频播放量+1
	if err := video.UpdateVideoPlayNum(mysql.DB, in.VideoId); err != nil {
		return nil, err
	}

	show := video.VideoShow(mysql.DB, in)

	return &shortrpc.VideoShowResponse{
		VideoId:       show.VideoId,
		VideoTitle:    show.VideoTitle,
		VideoCategory: show.VideoCategory,
		VideoLink:     show.VideoLink,
		VideoImage:    show.VideoImage,
		VideoIntro:    show.VideoIntro,
		PlayNum:       show.PlayNum,
		HeadImage:     show.HeadImage,
		NickName:      show.NickName,
		TopicName:     show.TopicName,
	}, nil
}
